package com.designMode.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//动态代理
public class DynamicProxyTest {
    public static void main(String[] args) {
        User user = new User();
        ByInterFace proxy = (ByInterFace) ProxyFactory.getProxy(user);

        proxy.by();
    }
}



class ProxyHandler implements InvocationHandler {

    private Object target;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法前代理>>>>>");
        Object res = method.invoke(target, args);
        System.out.println("方法后代理>>>>");

        return res;
    }

    public ProxyHandler(Object target) {
        this.target = target;
    }

    public ProxyHandler() {
    }
}

class ProxyFactory {
    public static Object getProxy(Object target) {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new ProxyHandler(target));
    }
}

class User implements ByInterFace{

    public void by() {
        System.out.println("方法执行!!!!!");
    }
}
